local zongyue = fk.CreateSkill {
  name = "hanqing__zongyue",
  tags = { Skill.Compulsory },
}

Fk:loadTranslationTable{
  ["hanqing__zongyue"] = "宗约",
  [":hanqing__zongyue"] = "锁定技，若你横置，你受到伤害/回复体力时，<a href='hanqing__alliance_href'>合纵/连横</a>状态"..
  "与你相同的其他角色横置/各摸一张牌。",
}

local U = require "packages/hanqing/util"

zongyue:addEffect(fk.DamageInflicted, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(zongyue.name) and
      player.chained and table.find(player.room:getOtherPlayers(player, false), function (p)
        return U.getAlliance(player) == U.getAlliance(p)
      end)
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getOtherPlayers(player), function (p)
      return U.getAlliance(player) == U.getAlliance(p)
    end)
    for _, p in ipairs(targets) do
      if not p.dead and not p.chained then
        p:setChainState(true)
      end
    end
  end,
})

zongyue:addEffect(fk.PreHpRecover, {
  anim_type = "support",
  can_trigger = function(self, event, target, player, data)
    return target == player and player:hasSkill(zongyue.name) and
      player.chained and table.find(player.room:getOtherPlayers(player, false), function (p)
        return U.getAlliance(player) == U.getAlliance(p)
      end) and not data.prevented
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local targets = table.filter(room:getOtherPlayers(player), function (p)
      return U.getAlliance(player) == U.getAlliance(p)
    end)
    for _, p in ipairs(targets) do
      if not p.dead then
        p:drawCards(1, zongyue.name)
      end
    end
  end,
})

return zongyue
